package Plugin_Reprise.editors.my_uvcmd_editor;

import java.util.Vector;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.rules.FastPartitioner;
import org.eclipse.jface.text.rules.IPartitionTokenScanner;

public class UVPartitionner extends FastPartitioner {

	public UVPartitionner(IPartitionTokenScanner scanner,
			String[] legalContentTypes) {
		
		
		super(scanner, legalContentTypes);
		// TODO Auto-generated constructor stub
	}
@Override
public void connect(IDocument document, boolean delayInitialization) {
	// TODO Auto-generated method stub
	super.connect(document, delayInitialization);
	GetUvcmds(document);
}

public Vector<String> GetUvcmds(IDocument document)
{

    StringBuffer commentaires = new StringBuffer();
    StringBuffer uvcmd = new StringBuffer();
    Vector<String> uvcmds=new Vector<String>();
    Vector<String> results=new Vector<String>();
    ITypedRegion[] partitions = computePartitioning(0, document.getLength());
    for (int i = 0; i < partitions.length; i++)
    {
        try
        {
        	if (partitions[i].getType()==EditorPartitionScannerUV.UV_COMMENT){
        		commentaires.append(document.get(partitions[i].getOffset(),partitions[i].getLength()));
        		commentaires.append('\n');
        	}else if (partitions[i].getType()==EditorPartitionScannerUV.UV_ALIAS){
        		String aliasdetecte=document.get(partitions[i].getOffset(),partitions[i].getLength());
        		if (aliasdetecte.equals("&erp.")) uvcmd.append("erp_mli.");
        		if (aliasdetecte.equals("&terp.")) uvcmd.append("terp_mli.");
        		if (aliasdetecte.equals("&outils.")) uvcmd.append("outils_mli.");
        		if (aliasdetecte.equals("&toutils.")) uvcmd.append("toutils_mli.");
        		
        	}else if (partitions[i].getType()==EditorPartitionScannerUV.UV_TAG){
        		uvcmd.append(document.get(partitions[i].getOffset(),partitions[i].getLength()));
        	}else if (partitions[i].getType()==EditorPartitionScannerUV.UV_SEPARATOR){
        		uvcmds.add(uvcmd+"");

        		uvcmd = new StringBuffer();
        	}
           /* buffer.append("Partition type: " 
              + partitions[i].getType() 
              + ", offset: " + partitions[i].getOffset()
              + ", length: " + partitions[i].getLength());
            buffer.append("\n");
            buffer.append("Text:\n");
            buffer.append(document.get(partitions[i].getOffset(), 
             partitions[i].getLength()));
            buffer.append("\n---------------------------\n\n\n");*/
        }
        catch (BadLocationException e)
        {
           /* e.printStackTrace();*/
        }
    }
    uvcmds.add(uvcmd+"");
    for (String req:uvcmds){
    	String commanduv=req.replaceAll("\r\n", " ").replaceAll("\n", " ").trim();
    	if (!commanduv.equals("")){
	    	System.out.println("------------");
	    	System.out.println(commanduv);   
	    	results.add(commanduv);
    	}

    }
    return results;
    /*System.out.print(buffer);*/
}


}
